import urlConfig from './config.js'

const request = {}
const headers = {}
const PORT1 = '/baseinfo'

request.globalRequest = (url, method, data, power) => {
	/*     权限判断 因为有的接口请求头可能需要添加的参数不一样，所以这里做了区分
	    1 == 不通过access_token校验的接口
	    2 == 文件下载接口列表
	    3 == 验证码登录 */
	// switch (power) {
	// 	case 1:
	// 		headers['Authorization'] = 'Basic a3N1ZGk6a3N1ZGk='
	// 		break;
	// 	default:
	// 		headers['Authorization'] = `Bearer ${
	//                this.$store.getters.userInfo
	//            }`
	// 		headers['TENANT-ID'] = this.$store.getters.userInfo.tenant_id
	// 		break;
	// }

	return uni.request({
		url: urlConfig + url,
		method,
		data: data,
		dataType: 'json',
		header: headers
	}).then(res => {
		return res
	}).catch(parmas => {
		switch (parmas.code) {
			case 401:
				uni.clearStorageSync()
				break
			default:
				uni.showToast({
					title: parmas.message,
					icon: 'none'
				})
				return Promise.reject()
				break
		}
	})
}
request.post = (url, data, power) => {
	/*     权限判断 因为有的接口请求头可能需要添加的参数不一样，所以这里做了区分
	    1 == 不通过access_token校验的接口
	    2 == 文件下载接口列表
	    3 == 验证码登录 */
	// switch (power) {
	// 	case 1:
	// 		headers['Authorization'] = 'Basic a3N1ZGk6a3N1ZGk='
	// 		break;
	// 	default:
	// 		headers['Authorization'] = `Bearer ${
	//                 this.$store.getters.userInfo
	//             }`
	// 		headers['TENANT-ID'] = this.$store.getters.userInfo.tenant_id
	// 		break;
	// }
	let requestUrl
	//console.log(request.baseUrl,'请求地址---------员工端----------------------',urlObject.port)
	if (urlConfig.includes('www')) {
		//console.log('正式环境---------------')
		requestUrl = urlConfig + url;
	} else if (urlConfig.includes("132") && process.env.NODE_ENV == 'production') {
		//console.log('测试环境---------------')
		//#ifdef H5
		const ipAddress = window.location.href
		const urlObject = new URL(ipAddress)
		const baseUrl = urlObject.origin + urlObject.port
		requestUrl = `http://192.168.101.132:${urlObject.port}` + '/api' + url;
		//#endif
		//#ifdef APP-PLUS
		requestUrl = urlConfig + url;
		//#endif
	} else {
		requestUrl = urlConfig + url
	}
	// console.log("requestUrl",requestUrl);
	return uni.request({
		url: requestUrl,
		method: 'post',
		data: data,
		dataType: 'json',
		header:{
			'csrfToken':uni.getStorageSync("csrf-token"),
		}
	}).then(res => {
		return res[1]
	}).catch(parmas => {

	})
}
request.get = (url, data, power) => {
	/*     权限判断 因为有的接口请求头可能需要添加的参数不一样，所以这里做了区分
	    1 == 不通过access_token校验的接口
	    2 == 文件下载接口列表
	    3 == 验证码登录 */

	// 设置head
	// switch (power) {
	// 	case 1:
	// 		headers['Authorization'] = 'Basic a3N1ZGk6a3N1ZGk='
	// 		break;
	// 	default:
	// 		headers['Authorization'] = `Bearer ${
	//                  this.$store.getters.userInfo
	//              }`
	// 		headers['TENANT-ID'] = this.$store.getters.userInfo.tenant_id
	// 		break;
	// }
	let requestUrl
	//console.log(request.baseUrl,'请求地址---------员工端----------------------',urlObject.port)
	if (urlConfig.includes('www')) {
		//console.log('正式环境---------------')
		requestUrl = urlConfig + url;
	} else if (urlConfig.includes("132") && process.env.NODE_ENV == 'production') {
		//console.log('测试环境---------------')
		//#ifdef H5
		const ipAddress = window.location.href
		const urlObject = new URL(ipAddress)
		const baseUrl = urlObject.origin + urlObject.port
		requestUrl = `http://192.168.101.132:${urlObject.port}` + '/api' + url;
		//#endif
		//#ifdef APP-PLUS
		requestUrl = urlConfig + url;
		//#endif
	} else {
		requestUrl = urlConfig + url
	}
	return uni.request({
		url: requestUrl,
		method: 'get',
		data: data,
		dataType: 'json',
		header: {
			uuid: uni.getStorageSync("uuid"),
			'csrfToken':uni.getStorageSync("csrf-token"),
		}
	}).then(res => {
		return res[1]
	}).catch(parmas => {

	})
}
export default request